<?php include 'login/Restrito.php';?>
<?php

	$horarios = array('08:00', '08:30', '09:00', '09:30', '10:00', '10:30', '11:00','11:30', '12:00', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00');
	if (isset($_POST['data'])) {
		$data = convertDataEmAmd($_POST['data']);;
	}else{
		$data = date('Y-m-d');
	}
	
	
	require_once  "banco/ConexaoBanco.php";
	function convertDataEmAmd($dt)
	{
		$dia = substr($dt,0,2);
		$mes = substr($dt,3,2);
		$ano = substr($dt,6,4);
		$data = $ano."-".$mes."-".$dia;
		return $data;
	}
?>


	<div align="center">
		<h3 align="center">Agenda de Consultas</h3>
  		<hr color="#0066FF" size="3px">
  		<form method="POST" action="#">
		<p>Escolher data: <input type="text" id="datepicker" name="data" size="30" /> <input type="submit" value="Mostrar" class="botao"></p>
		</form>
		
		<p align="left" style="margin-left: 30px;">Data: <strong><?php if (isset($_POST['data'])) {echo $_POST['data'];}else{echo date('d-m-Y');}?></strong></p>
		<table width="900" border="1" id="tabela" align="center" cellpadding="4">
			
			<tr  bgcolor="#BBBBBB" align="center" height="35px">
				<td width="50px">Horário</td>
				<td>Paciente</td>
				<td>Médico</td>
				<td>Tipo</td>
				<td>Status</td>
				<td>Action</td>
			</tr>
			
			<?php 
				
			for ($i = 0; $i < 18; $i++){
				
				$sql = mysql_query("SELECT p_nome, ag_tipo_consulta, ag_status, m_nome, ag_hora, id_agendamento FROM agendamento, paciente, medico WHERE ag_data = '$data' AND agendamento.paciente_id = paciente.id_paciente AND agendamento.medico_id = medico.id_medico");
				//$dados = mysql_fetch_array($sql);
				
				
				if(mysql_num_rows($sql) == 0 ){
					echo'<tr align="center">
							<td >'.$horarios[$i].'</td>
							<td>XXXXX</td>
							<td>XXXXX</td>
							<td>XXXXX</td>
							<td>XXXXX</td>
							<td><a href="?p=marcar-consulta&d='.$data.'&h='.$horarios[$i].'">Marcar</a></td>
						</tr>
						';
				}
				//$query = mysql_query("SELECT p_nome FROM paciente WHERE id_paciente = ".$dados['paciente_id']);
				while($lista = mysql_fetch_assoc($sql)) {
					if($lista['ag_hora'] == $horarios[$i].':00' && $lista['ag_status']=='Ativo'){
  						echo '<tr align="center">
							<td style="background:#3CD571;">'.$horarios[$i].'</td>
  							<td style="background:#3CD571;">'.$lista['p_nome'].'</td>
							<td style="background:#3CD571;">'.$lista['m_nome'].'</td>
							<td style="background:#3CD571;">'.$lista['ag_tipo_consulta'].'</td>
							<td style="background:#3CD571;">'.$lista['ag_status'].'</td>
							<td style="background:#3CD571;"><a href="?p=cancelar-consulta&id='.$lista['id_agendamento'].'">Cancelar</a></td>
						</tr>
						';
					}
					if($lista['ag_hora'] == $horarios[$i].':00' && $lista['ag_status']=='Cancelado'){
						echo '<tr align="center">
							<td style="background:#CD5C5C;">'.$horarios[$i].'</td>
  							<td style="background:#CD5C5C;">'.$lista['p_nome'].'</td>
							<td style="background:#CD5C5C;">'.$lista['m_nome'].'</td>
							<td style="background:#CD5C5C;">'.$lista['ag_tipo_consulta'].'</td>
							<td style="background:#CD5C5C;">'.$lista['ag_status'].'</td>
							<td style="background:#CD5C5C;"></td>
							</tr>
						';
					}
					/* else{
						echo'<td >'.$horarios[$i].'</td>
							<td>XXXXX</td>
							<td>XXXXX</td>
							<td>XXXXX</td>
							<td>XXXXX</td>
							<td><a href="?p=marcar-consulta&d='.$data.'&h='.$horarios[$i].'">Marcar</a></td>
						</tr>
						';
					} */
				}
				
				
			} 	?>
			</table>
	</div>
	<br><br>
	
	